package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.Car;
import com.system.runtai.entity.CarClassify;
import com.system.runtai.entity.CompanyDynamic;
import com.system.runtai.service.CarClassifyService;
import com.system.runtai.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆分类 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "车辆分类")
@RestController
@RequestMapping("/runtai/car-classify")
public class CarClassifyController {

    @Autowired
    private CarClassifyService carClassifyService;

    @Autowired
    private CarService carService;

    @ApiOperation(value = "所有车辆分类")
    @GetMapping("/carClassifyList")
    public Result<List<CarClassify>> carClassifyList(){

        List<CarClassify> list = carClassifyService.list();

        return Result.ok(list);
    }


    @ApiOperation(value = "添加车辆分类")
    @PostMapping("/saveCarClassify")
    public Result<CarClassify> saveCarClassify(@RequestBody CarClassify carClassify){

        carClassify.setCreateTime(LocalDateTime.now());
        boolean save = carClassifyService.save(carClassify);

        if (save){
            return Result.ok(carClassify).message("添加成功");
        }else {
            return Result.ok(carClassify).message("添加失败");
        }
    }


    @ApiOperation(value = "修改车辆分类")
    @PostMapping("/updateCarClassify")
    public Result<CarClassify> updateCarClassify(@RequestBody CarClassify carClassify){

        carClassify.setUpdateTime(LocalDateTime.now());
        boolean update = carClassifyService.updateById(carClassify);

        if (update){
            return Result.ok(carClassify).message("修改成功");
        }else {
            return Result.ok(carClassify).message("修改失败");
        }
    }


    @ApiOperation(value = "删除车辆分类")
    @PostMapping("delectCarClassify/{id}")
    public Result delectCarClassify(@PathVariable Integer id){

        boolean b = carClassifyService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询车辆分类")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CarClassify>> index(@PathVariable Long page, @PathVariable Long limit, CarClassify carClassify){

        Page<CarClassify> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CarClassify> wrapper = new LambdaQueryWrapper<>();

        Integer id = carClassify.getId();

        if (id!=null){
            wrapper.eq(CarClassify::getId,id);
        }

        IPage<CarClassify> iPage = carClassifyService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

}

